package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿管理
 */
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "C端-地址簿相关接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址簿
     */
    @PostMapping
    @ApiOperation("新增地址簿")
    public Result save(@RequestBody AddressBook addressBook) {
        log.info("新增地址簿信息：{}", addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 查询地址簿
     */
    @GetMapping("/list")
    @ApiOperation("查询地址簿")
    public Result<List<AddressBook>> list(){
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(userId);
        List<AddressBook> list =  addressBookService.list(addressBook);
        return Result.success(list);
    }

    /**
     * 查询地址簿单条
     */
    @GetMapping("/{label}")
    @ApiOperation("查询地址簿单条")
    public Result<AddressBook> getById(@PathVariable String label,Long id){
        log.info("查询地址簿单条：{},{}", label,id);
        AddressBook addressBook =  addressBookService.getById(label,id);
        return Result.success(addressBook);
    }

    /**
     *编辑地址簿
     */
    @PutMapping
    @ApiOperation("编辑地址簿")
    public Result updateAddressBook(@RequestBody  AddressBook addressBook ){
        log.info("编辑地址簿：{}", addressBook);
        addressBookService.update(addressBook);
    return  Result.success();
    }

    /**
     * 删除地址簿单条
     */
    @DeleteMapping()
    @ApiOperation("删除地址簿")
    public Result delete(Long id){
        addressBookService.delete(id);
        return Result.success();
    }

    /**
     * 设为默认地址
     */
    @PutMapping("/default")
    @ApiOperation("设为默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
//        addressBook.setIsDefault(StatusConstant.ENABLE);
        log.info("更新默认地址，{}",addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder().userId(userId).isDefault(StatusConstant.ENABLE).build();
        List<AddressBook> list = addressBookService.list(addressBook);
        if(list != null && list.size()>0){
            return Result.success(list.get(0));
        }
        return Result.success();
    }
}
